1 using System.Collections;
2 using
System.IO;
3 using
System.Xml;
4 using
System.Xml.Linq;
5
6 using
UnityEditor;
7 using
UnityEngine;
8
9 namespace
Tiled2Unity
10 {
11     
// Assets that are imported to "Assets/Tiled2Unity/..." will use this post processor
12     
public class TiledAssetPostProcessor : AssetPostprocessor
13     {
14         
private static bool IsTiled2UnityFile(string assetPath)
15         {
16             
return UseThisImporter(assetPath) && assetPath.EndsWith(".tiled2unity.xml");
17         }
18
19         
private static bool IsTiled2UnityTexture(string assetPath)
20         {
21             
bool startsWith = assetPath.StartsWith("Assets/Tiled2Unity/Textures/");
22             
bool endsWithTxt = assetPath.EndsWith(".txt");
23             
return startsWith && !endsWithTxt;
24         }
25
26         
private static bool IsTiled2UnityWavefrontObj(string assetPath)
27         {
28             
bool startsWith = assetPath.StartsWith("Assets/Tiled2Unity/Meshes/");
29             
bool endsWith = assetPath.EndsWith(".obj");
30             
return startsWith && endsWith;
31         }
32
33         
private static bool IsTiled2UnityPrefab(string assetPath)
34         {
35             
bool startsWith = assetPath.StartsWith("Assets/Tiled2Unity/Prefabs/");
36             
bool endsWith = assetPath.EndsWith(".prefab");
37             
return startsWith && endsWith;
38         }
39
40         
private static bool UseThisImporter(string assetPath)
41         {
42             
return assetPath.StartsWith("Assets/Tiled2Unity");
43         }
44
45         
private bool UseThisImporter()
46         {
47             
return UseThisImporter(this.assetPath);
48         }
49
50         
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
51         {
52             
foreach (string imported in importedAssets)
53             {
54                 
if (UseThisImporter(imported))
55                 {
56                     
//Debug.Log(string.Format("Imported: {0}", imported));
57                 }
58                 
else
59                 {
60                     
continue;
61                 }
62
63                 
using (ImportTiled2Unity t2uImporter = new ImportTiled2Unity())
64                 {
65                     
if (IsTiled2UnityFile(imported))
66                     {
67                         
// Start the import process by importing our textures and meshes
68                         t2uImporter.XmlImported(imported);
69                     }
70                     
else if (IsTiled2UnityTexture(imported))
71                     {
72                         
// A texture was imported and the material assigned to it may need to be fixed
73                         t2uImporter.TextureImported(imported);
74                     }
75                     
else if (IsTiled2UnityWavefrontObj(imported))
76                     {
77                         t2uImporter.MeshImported(imported);
78                     }
79                     
else if (IsTiled2UnityPrefab(imported))
80                     {
81                         Debug.Log(
string.Format("Imported prefab from Tiled map editor: {0}", imported));
82                     }
83                 }
84             }
85         }
86
87         
private void OnPreprocessModel()
88         {
89             
if (!UseThisImporter())
90                 
return;
91
92             ModelImporter modelImporter =
this.assetImporter as ModelImporter;
93
94             
// Keep normals otherwise Unity will complain about needing them.
95             
// Normals may not be a bad idea anyhow
96             modelImporter.normalImportMode = ModelImporterTangentSpaceMode.Import;
97
98             
// Don't need animations or tangents.
99             modelImporter.generateAnimations = ModelImporterGenerateAnimations.None;
100             modelImporter.animationType = ModelImporterAnimationType.None;
101             modelImporter.tangentImportMode = ModelImporterTangentSpaceMode.None;
102
103             
// Do not need mesh colliders on import.
104             modelImporter.addCollider =
false;
105
106             
// We will create and assign our own materials.
107             
// This gives us more control over their construction.
108             modelImporter.importMaterials =
false;
109         }
110
111         
private void OnPostprocessModel(GameObject gameObject)
112         {
113             
if (!UseThisImporter())
114                 
return;
115
116             
// Each mesh renderer has the ability to set the a sort layer but it takes some work with Unity to expose it.
117             
foreach (MeshRenderer mr in gameObject.GetComponentsInChildren<MeshRenderer>())
118             {
119                 mr.gameObject.AddComponent<SortingLayerExposed>();
120
121                 
// Also, no shadows
122                 mr.receiveShadows =
false;
123                 mr.castShadows =
false;
124             }
125         }
126
127         
private Material OnAssignMaterialModel(Material defaultMaterial, Renderer renderer)
128         {
129             
if (!UseThisImporter())
130                 
return null;
131
132             
// This is the only reliable place to assign materials in the import chain.
133             
// It kind of sucks because we have to go about making the mesh/material association in a roundabout way.
134
135             
// Note: This seems dangerous, but getting to the name of the base gameObject appears to be take some work.
136             
// The root gameObject, at this point, seems to have "_root" appeneded to it.
137             
// Once the model if finished being imported it drops this postifx
138             
// This is something that could change without our knowledge
139             
string rootName = renderer.transform.root.gameObject.name;
140             
int rootIndex = rootName.LastIndexOf("_root");
141             
if (rootIndex != -1)
142             {
143                 rootName = rootName.Remove(rootIndex);
144             }
145
146             ImportTiled2Unity importer =
new ImportTiled2Unity();
147             
return importer.FixMaterialForMeshRenderer(rootName, renderer);
148         }
149
150         
private void OnPreprocessTexture()
151         {
152             
if (!UseThisImporter())
153                 
return;
154
155             TextureImporter textureImporter =
this.assetImporter as TextureImporter;
156             textureImporter.textureType = TextureImporterType.Default;
157             textureImporter.npotScale = TextureImporterNPOTScale.None;
158             textureImporter.convertToNormalmap =
false;
159             textureImporter.lightmap =
false;
160             textureImporter.alphaIsTransparency =
true;
161             textureImporter.grayscaleToAlpha =
false;
162             textureImporter.linearTexture =
false;
163             textureImporter.spriteImportMode = SpriteImportMode.None;
164             textureImporter.mipmapEnabled =
false;
165             textureImporter.generateCubemap = TextureImporterGenerateCubemap.None;
166             textureImporter.filterMode = FilterMode.Point;
167             textureImporter.wrapMode = TextureWrapMode.Clamp;
168             textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
169         }
170
171     }
172 }


Assets that are imported to "AssetsTiled2Unity..." will use this post processor

Debug.Log(string.Format("Imported: {0}", imported));

Start the import process by importing our textures and meshes

A texture was imported and the material assigned to it may need to be fixed

Keep normals otherwise Unity will complain about needing them.

Normals may not be a bad idea anyhow

Don't need animations or tangents.

Do not need mesh colliders on import.

We will create and assign our own materials.

This gives us more control over their construction.

Each mesh renderer has the ability to set the a sort layer but it takes some work with Unity to expose it.

Also, no shadows

This is the only reliable place to assign materials in the import chain.

It kind of sucks because we have to go about making the meshmaterial association in a roundabout way.

Note: This seems dangerous, but getting to the name of the base gameObject appears to be take some work.

The root gameObject, at this point, seems to have "_root" appeneded to it.

Once the model if finished being imported it drops this postifx

This is something that could change without our knowledge




Trò chơi đua xe động vật trong UNITY Engine 114.913 lượt xem

Gõ tìm kiếm nhanh...